﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataLib.Accounts.Bus;
using System.Configuration;
using System.Drawing;


namespace DataLib.Accounts
{
    public partial class UserUpdate : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                //BindSuppData();								
                User currentUser;
                if (Request["userid"] != null)
                {
                    int userid = int.Parse(Request["userid"]);
                    currentUser = new User(userid);
                    if (currentUser == null)
                    {
                        Response.Write("<script language=javascript>window.alert('该用户不存在！\\');history.back();</script>");
                        return;
                    }

                    this.lblName.Text = currentUser.UserName;
                    txtTrueName.Text = currentUser.TrueName;
                    if (currentUser.Sex == "男")
                        RadioButton1.Checked = true;
                    else
                        RadioButton2.Checked = true;
                    this.txtPhone.Text = currentUser.Phone;
                    //归属城市
                    ListItem item = new ListItem();
                    item.Text = "--请选择--";
                    item.Value = "";
                    BLL.City bllcity = new BLL.City();
                    dropBelongArea.DataSource = bllcity.GetList(" flag1 = '01'");
                    dropBelongArea.DataTextField = "cityname";
                    dropBelongArea.DataValueField = "cityname";
                    dropBelongArea.DataBind();
                    dropBelongArea.Items.Add(item);


                    txtEmail.Text = currentUser.Email;
                    dropBelongArea.SelectedValue = currentUser.CompanyCity;
                    dropDepart.SelectedValue = currentUser.Department;
                    dropUserType.SelectedValue = currentUser.Duty;
                   

                    //for(int i=0;i<this.Dropdepart.Items.Count;i++)
                    //{
                    //    if(this.Dropdepart.Items[i].Value==currentUser.DepartmentID)
                    //    {
                    //        this.Dropdepart.Items[i].Selected=true;
                    //    }
                    //}


                    this.dropStyle.SelectedIndex = currentUser.Style - 1;

                    AccountsPrincipal user = new AccountsPrincipal(userid);
                    BindRoles(user);


                }
            }
        }

        private void BindRoles(AccountsPrincipal user)
        {
            if (user.PermissionsDesc.Count > 0)
            {
                RoleList.Visible = true;
                RoleList.Text = "权限列表：<ul>";
                foreach (string perm in user.PermissionsDesc)
                {
                    RoleList.Text += "<li>" + perm + "</li>";
                }
                RoleList.Text += "</ul>";
            }
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            string username = this.lblName.Text.Trim();
            AccountsPrincipal user = new AccountsPrincipal(username);
            User currentUser = new DataLib.Accounts.Bus.User(user);

            currentUser.UserName = username;
            currentUser.TrueName = txtTrueName.Text.Trim();
            if (txtPassword.Text.Trim() != "")
            {
                currentUser.Password = AccountsPrincipal.EncryptPassword(txtPassword.Text);
            }
            if (RadioButton1.Checked)
                currentUser.Sex = "男";
            else
                currentUser.Sex = "女";
            currentUser.Phone = this.txtPhone.Text.Trim();
            currentUser.Email = txtEmail.Text.Trim();
            //currentUser.EmployeeID=0;
            //currentUser.DepartmentID=this.Dropdepart.SelectedValue;			          
            int style = int.Parse(this.dropStyle.SelectedValue);
            currentUser.Style = style;
            currentUser.CompanyCity = dropBelongArea.SelectedValue;
            currentUser.Duty = dropUserType.SelectedValue;
            currentUser.Department = dropDepart.SelectedValue;
            if (!currentUser.Update())
            {
                this.lblMsg.ForeColor = Color.Red;
                this.lblMsg.Text = "更新用户信息发生错误！";
            }
            else
            {
			 Response.Write("<script>alert('用户信息更新成功!');window.location.href='UserAdminList.aspx' ;</script>");
                //Response.Redirect("UserAdminList.aspx");
            }
        }
    }

}